package com.dfrz.planeclub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dfrz.planeclub.mapper.UserMapper;
import com.dfrz.planeclub.pojo.User;
import com.dfrz.planeclub.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author PuXiaLing
 * @description: TODO
 * @date 2024/4/11 9:21
 */
@Service
public class UseServiceImpl implements UserService {

    /**
     *
     */
    @Resource
    UserMapper userMapper;



    /**
     * 用户账号密码登录
     * @param loginer 登录用户账号
     * @return 用户所有信息
     */
    @Override
    public User accLogin(User loginer) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
//       通过账号查用户
        wrapper.eq("account",loginer.getAccount());
        User user=userMapper.selectOne(wrapper);
        if (user!=null&&user.getPassword().equals(loginer.getPassword())){
            return user;
        }
        return null;
    }

    /**
     * 检查手机是佛被注册
     * @param phone 手机号
     */
    @Override
    public User getPhone(String phone) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
//       通过手机查用户
        wrapper.eq("phone",phone);
        User user=userMapper.selectOne(wrapper);
        if(user!=null){
            return user;
        }
        return null;
    }

    /**
     * 检查用户名、手机、账号是否重复
     * @param users
     * @return
     */
    @Override
    public Map<String, Object> check(User users) {
        Map<String, Object> map=new HashMap<>();
        //检查账号
        if (!users.getAccount().equals("")){
            QueryWrapper<User> wrapper=new QueryWrapper<>();
            wrapper.eq("account",users.getAccount());
            User user=userMapper.selectOne(wrapper);
            if(user!=null){
                map.put("account",false);
            }else {
                map.put("account",true);
            }
        }
        //检查手机号
        if (!users.getPhone().equals("")){
            QueryWrapper<User> wrapper=new QueryWrapper<>();
            wrapper.eq("phone",users.getPhone());
            User user=userMapper.selectOne(wrapper);
            if(user!=null){
                map.put("phone",false);
            }else {
                map.put("phone",true);
            }
        }
        //检查用户名
        if (!users.getName().equals("")){
            QueryWrapper<User> wrapper=new QueryWrapper<>();
            wrapper.eq("name",users.getName());
            User user=userMapper.selectOne(wrapper);
            if(user!=null){
                map.put("name",false);
            }else {
                map.put("name",true);
            }
        }
        System.out.println(map);
        return map;
    }

    /**
     * 注册添加信息
     * @param users 添加注册用户的信息
     * @return
     */
    @Override
    public User regist(User users) {
        int i=userMapper.insert(users);
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("name",users.getName());
        User user=userMapper.selectOne(wrapper);
        return user;
    }

    /**
     * 更新用户头像地址
     * @param user 更新
     */
    @Override
    public boolean update(User user) {
        int i=userMapper.updateById(user);
        return i>0;
    }


}
